package com.bsh.api;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bsh.api.Exception.ApiException;
import com.bsh.service.ApiUpgradeService;
import com.bsh.utils.ApiMessage;
import com.bsh.utils.IpUtil;

/**
 * 会员升级相关,如 vip,gold
 * 
 * @author jane
 * @since 0.0.1
 * 
 */
@Controller
@RequestMapping(value = "/api/v1/upgrade", method = { RequestMethod.POST })
public class ApiUpgradeController {
	@Autowired
	HttpServletRequest request;
	@Autowired
	HttpServletResponse response;
	@Autowired
	HttpSession session;
	@Autowired
	ApiUpgradeService apiUpgradeService;
	
	@SuppressWarnings("finally")
	@RequestMapping(value = "/member/balance", method = { RequestMethod.POST })
	@ResponseBody
	public ApiMessage gradeBalance(@RequestParam String paramStr) {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			dto.put("ip", IpUtil.getIpAddr(request));
			msg = apiUpgradeService.gradeBalance(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
			e.printStackTrace();
		} finally {
			return msg;
		}
	
	}
 
	/**
	 * Api 会员在线支付 升级(只返回流水号) 不处理回调业务
	 * @param paramStr
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/member/online", method = { RequestMethod.POST })
	@ResponseBody
	public ApiMessage gradeOnline(@RequestParam String paramStr) {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			dto.put("ip", IpUtil.getIpAddr(request));
			msg = apiUpgradeService.gradeOnline(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
			e.printStackTrace();
		} finally {
			return msg;
		}
		
	}
}
